package com.example.yunhailutong.zhongtan.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.example.yunhailutong.zhongtan.Adapter.CsfcListViewAdapter;
import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.activity.MessageDetailsActivity;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.entity.CsfcEntity;
import com.example.yunhailutong.zhongtan.entity.CsfcEntity.DataBean.CsfcInfoBean;
import com.example.yunhailutong.zhongtan.net.PersionBiz;
import com.example.yunhailutong.zhongtan.weight.RoundImageView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class ChengShiFengCaiFragment extends BaseFragment {
    View view;
    @BindView(R.id.csfc_listview)
    ListView listView;
    Unbinder unbinder;

    @BindView(R.id.csfc_top_imageview)
    RoundImageView csfcBanner;
    private CsfcListViewAdapter listViewAdapter;
    private List<CsfcInfoBean> lstBanner;
    private List<CsfcInfoBean> lstCsfc;

    public ChengShiFengCaiFragment() {
        // Required empty public constructor
    }
    @Override
    public void onResume() {
        super.onResume();
        new PersionBiz().getCsfcInfo();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_cheng_shi_feng_cai, container, false);
        unbinder = ButterKnife.bind(this, view);
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }

        new PersionBiz().getCsfcInfo();

        return view;
    }

    @OnClick({R.id.csfc_flzx_imageview, R.id.csfc_xllx_imageview, R.id.csfc_xlsq_imageview,
                R.id.csfc_zdry_imageview, R.id.csfc_top_imageview})
    @Override
    public void onClick(View v) {

        if (v instanceof ImageView) {
            switch (v.getId()) {
                case R.id.csfc_flzx_imageview:
                    break;
                case R.id.csfc_xllx_imageview:
                    break;
                case R.id.csfc_xlsq_imageview:
                    break;
                case R.id.csfc_zdry_imageview:
                    break;
                case R.id.csfc_top_imageview:
                    Intent intent = new Intent(view.getContext(), MessageDetailsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("content", lstBanner.get(0).getIm_content());
                    bundle.putString("title", lstBanner.get(0).getIm_title());
                    bundle.putString("img_url", lstBanner.get(0).getIm_imgPath());
                    intent.putExtras(bundle);
                    view.getContext().startActivity(intent);
                    break;
                    default:
                        break;
            }
        }
    }

    @Subscribe
    public void log(OldsEvent.CsfcEvent msg) {
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        } else if (msg.what == 1) {
            updateView((CsfcEntity) msg.obj);
        }
    }

    private void updateView(CsfcEntity csfcEntity) {
        lstBanner = csfcEntity.getData().getLstBanner();
        lstCsfc = csfcEntity.getData().getLstCsfc();
        if (lstBanner.get(0).getIm_imgPath() != null) {
            Glide.with(getContext()).load(lstBanner.get(0).getIm_imgPath()).into(csfcBanner);
        }
        listViewAdapter = new CsfcListViewAdapter(getContext(), lstCsfc);
        listView.setAdapter(listViewAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CsfcInfoBean csfcInfoBean = lstCsfc.get(position);
                Intent intent = new Intent(view.getContext(), MessageDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("content", csfcInfoBean.getIm_content());
                bundle.putString("title", csfcInfoBean.getIm_title());
                bundle.putString("img_url", csfcInfoBean.getIm_imgPath());
                intent.putExtras(bundle);
                view.getContext().startActivity(intent);
            }
        });
    }
}
